<%--
  Created by IntelliJ IDEA.
  User: 檬zhu
  Date: 2021/12/15
  Time: 14:51
  To change this template use File | Settings | File Templates.
--%>
<%@page import="java.util.List" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="com.alibaba.fastjson.JSON" %>
<%@ page import="com.example.ordersystem.domain.DishInfo" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <link rel="stylesheet" href="/resources/layui/css/layui.css">
</head>
<body>
<jsp:include page="title.jsp"/>
<div class="cBody" style="margin-left:7%;">
    <div class="console">
        <form class="layui-form" action="${pageContext.request.contextPath}/merchant/selectDishInfoByKeyword"
              method="post">
            <div class="layui-form-item">
                <div class="layui-input-inline">
                    <input type="text" name="keyword" required lay-verify="required"
                           placeholder="输入关键字" autocomplete="off" class="layui-input">
                </div>
                <button class="layui-btn" lay-submit>检索</button>
            </div>
            <input type="hidden" name="previousPage" value="dishManagePage">
        </form>
    </div>

    <table class="layui-table" style="width: 88%;height: 70%">
        <thead>
        <tr>
            <th>序号</th>
            <th>菜品分类</th>
            <th>菜品名称</th>
            <th>价格</th>
            <th>图片</th>
            <th>菜品描述</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <%
            List<DishInfo> dishes;
            Object obj = request.getAttribute("dishInfo");

            if (obj == null) { //会话过期
                response.sendRedirect("/");
                return; //不再执行接下来的代码
            } else {

                if (obj instanceof JSONArray) {
                    dishes = JSON.parseArray(((JSONArray) obj).toJSONString(), DishInfo.class);
                } else {
                    dishes = (List<DishInfo>) obj;
                }
                session.setAttribute("total", dishes.size());
                int begin = Integer.parseInt(String.valueOf(session.getAttribute("begin"))); //从第begin条留言开始显示
                int end = Integer.parseInt(String.valueOf(session.getAttribute("end")));//一直显示到第end条留言
                for (int i = begin; i < dishes.size() && i <= end; ++i) {
        %>
        <tr>
            <td><%=i + 1%>
            </td>
            <td><%=dishes.get(i).getcName()%>
            </td>
            <td><%=dishes.get(i).getdName()%>
            </td>
            <td><%=dishes.get(i).getPrice()%>
            </td>
            <td><img style="height: 100px;" alt=""
                     src="<%=dishes.get(i).getDisplayPicture()%>"></td>
            <td width="350px;"><%=dishes.get(i).getDescription()%>
            </td>
            <td>
                <form action="${pageContext.request.contextPath}/merchant/editOrDeleteDish">
                    <input type="hidden" name="num" value="<%=dishes.get(i).getNum()%>">
                    <button class="layui-btn layui-btn-normal layui-btn-sm" name="edit" value="编辑">编辑</button>
                    <button onclick="javascript:return window.confirm('确认删除吗？')"
                            class="layui-btn layui-btn-danger layui-btn-sm" name="delete" value="删除">删除
                    </button>
                </form>
            </td>
        </tr>
        <%
                }
            }
        %>
        </tbody>
    </table>
</div>
<form action="${pageContext.request.contextPath}/merchant/pageShow" method="post"
      style="position: relative; margin-left: 40%;">
    <input type="hidden" name="nextPage" value="dishManagePage">
    <input type="submit" value="显示全部" name="all" class="layui-btn layui-btn-radius">
    <input type="submit" value="上一页" name="previous" class="layui-btn layui-btn-radius">
    <input type="submit" value="下一页" name="next" class="layui-btn layui-btn-radius">
</form>

<script
        src="${pageContext.request.contextPath}/resources/layui/layui.js"></script>
<script>
    //JavaScript代码区域
    layui.use('table', function () {
        const table = layui.table;
    });
</script>
</body>
</html>
